<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Article extends MY_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
		$this->load->library('tank_auth');
		$this->lang->load('tank_auth');
        if(!$this->tank_auth->is_logged_in())
            redirect('administrator/auth');
        $this->load->model('articles','art');
        $this->load->model('categories','cat');
        $this->load->model('tank_auth/roles','rol');
    }

    /**
     * Default function
     */
    public function index()
    {
		
    }
    
    public function posts($act=false){
        $post= $this->input->post(null);
            $this->_layout['action'] = $this->uri->segment(4);
            $this->_layout['post_id'] = $this->uri->segment(5);
            
        $this->assign('action','');
        $article = $this->art->get_article();
        $this->assign('article',$article);
        if($act)
        {
            $this->_layout['categories'] = $this->cat->getCat2();
            $this->_layout['roles'] = $this->rol->get_roles();
            if($act=="save"){
                if($post['title'] =="" || $post['content']=="" ){
                            _set_error_message('....CDF');
                            redirect('administrator/article/posts');
                        }
                switch ($post['action']) {
                    case 'create':                         
                        $posttitle = $this->_global->_serialize(array($this->session->userdata('lan')=>array('post_title'=>$post['title'])));
                        $postdesc = str_replace('cdn/',$this->config->item('cdn_url'),str_replace('../','',$post['post_desc']));
                        $postdesc = $this->_global->_serialize(array($this->session->userdata('lan')=>array('post_desc'=>$post['post_desc'])));
                        
                        $postcontent = str_replace('cdn/',$this->config->item('cdn_url'),str_replace('../','',$post['content']));
                        
                        $postcontent = $this->_global->_serialize(array($this->session->userdata('lan')=>array('post_content'=>$postcontent)));
                        $seotitle = $this->_global->_serialize(array($this->session->userdata('lan')=>array('seo_title'=>$post['seo_title'])));
                        if(!empty($post['seo_title']))
                            $seoalias = $this->_global->_serialize(array($this->session->userdata('lan')=>array('alias'=>_set_alias($post['seo_title']))));
                        else
                            $seoalias = $this->_global->_serialize(array($this->session->userdata('lan')=>array('alias'=>_set_alias($post['title']))));
                        if(!isset($_POST['feauter']))
                            $_POST['feauter'] =0;
                            
                        
                        $fields = array('cat_id'=>$post['cat_id']
                                    ,'user_id'=>$this->session->userdata('user_id')
                                    ,'post_title'=>$posttitle
                                    ,'post_date'=>time()
                                    ,'post_desc'=>$postdesc
                                    ,'post_content'=>$postcontent
                                    ,'metakey'=>$post['metakey']
                                    ,'metadesc'=>$post['metadesc']
                                    ,'tagline'=>$post['tagline']
                                    ,'publish_up'=>strtotime($post['publish_up'])
                                    ,'publish_down'=>strtotime($post['publish_down'])
                                    ,'seo_title'=>$seotitle
                                    ,'seo_alias'=>$seoalias
                                    ,'feature' =>$_POST['feauter']
                                    ,'access'=>$post['access']);
                        
                        $rs = $this->art->add_article($fields);
                        if($rs>0)
                            _set_success_message('Save successful');
                        else
                            _set_error_message('....');
                        redirect('administrator/article/posts');
                        
                        break;
                    case 'edit':
                        //lay gia tri da ton tai -> mang
                        $article = $this->art->get_article($post['post_id']);
                        $posttitle = $this->_global->_unserialize($article->post_title);
                        $postdesc = $this->_global->_unserialize($article->post_desc);
                        $postcontent = $this->_global->_unserialize($article->post_content);
                        $seotitle = $this->_global->_unserialize($article->seo_title);
                        $seoalias = $this->_global->_unserialize($article->seo_alias);
                        
                        if(!isset($posttitle[$this->session->userdata('lan')]))
                        {
                            //Nếu sản phẩm chưa tồn tại ngôn ngữ này
                        }else{
                            $posttitle[$this->session->userdata('lan')]['post_title']=$post['title'];
                            $d = str_replace('cdn/',$this->config->item('cdn_url'),str_replace('../','',$post['post_desc']));
                            $postdesc[$this->session->userdata('lan')]['post_desc']=$d;
                            $p =str_replace('cdn/',$this->config->item('cdn_url'),str_replace('../','',$post['content']));
                        
                            $postcontent[$this->session->userdata('lan')]['post_content']=$p;
                            $seotitle[$this->session->userdata('lan')]['seo_title']=$post['seo_title'];
                            $seoalias[$this->session->userdata('lan')]['alias']=_set_alias($post['seo_title']);
                        }
                        //update db
                        if(!isset($_POST['feauter']))
                            $_POST['feauter'] =0;
                        
                        $rs = $this->art->update_article(array('cat_id'=>$post['cat_id']
                                                                ,'user_id'=>$this->session->userdata('user_id')
                                                                ,'post_title'=>$this->_global->_serialize($posttitle)
                                                                ,'post_date'=>time()
                                                                ,'post_desc'=>$this->_global->_serialize($postdesc)
                                                                ,'post_content'=>$this->_global->_serialize(str_replace('\\&quote;','',$postcontent))
                                                                ,'metakey'=>$post['metakey']
                                                                ,'metadesc'=>$post['metadesc']
                                                                ,'tagline'=>$post['tagline']
                                                                ,'publish_up'=>strtotime($post['publish_up'])
                                                                ,'publish_down'=>strtotime($post['publish_down'])
                                                                ,'feature'=>$_POST['feauter']
                                                                ,'seo_title'=>$this->_global->_serialize($seotitle)
                                                                ,'seo_alias'=>$this->_global->_serialize($seoalias)
                                                                ,'access'=>$post['access']),array('post_id'=>$post['post_id']));                        
                        
                        if($rs>0)
                            _set_success_message('Save successful');
                        else
                            _set_error_message('....');
                        redirect('administrator/article/posts');
                        break;
                }
                
            }elseif($act=="delete"){
                $rs = $this->art->delete_article($this->_layout['post_id']);
                if($rs>0)
                            _set_success_message('Save successful');
                redirect('administrator/article/posts');
            }elseif($act=="edit"){
               $this->_layout['posts'] = $this->art->get_article($this->_layout['post_id']);
               
            }
            $form   = $this->load->view('layouts/form/posts',$this->_layout,true);
            $this->assign('action',$form);
            
        }
    }
    
}